package com.chen.service.Impl;

import com.chen.mapper.DictMapper;
import com.chen.pojo.Dict;
import com.chen.service.DictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chen

 */
@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    private static Logger log = LoggerFactory.getLogger(DictServiceImpl.class);

    private static final String DICT = "dict";

    @Override
    public List<Dict> getDict() {


        List<Dict> dicts = (List<Dict>) redisTemplate.opsForValue().get(DICT);
        if (dicts != null) {
            log.info("走的是redis");
            return dicts;

        } else {
            List<Dict> dictList = dictMapper.getDict();
            dictList = dictList.stream().distinct().collect(Collectors.toList());
            synchronized (dictList) {
                redisTemplate.opsForValue().set(DICT, dictList);
                log.info("走的是MYSQL");
                return dictList;
            }
        }


    }

    @Override
    public int addDict(Dict dict) {



        return 0;
    }
}
